package com.vvlive.caibao.smartcity.ui;

import android.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.bean.BusinessTripApply;
import com.vvlive.caibao.smartcity.utils.TimeUtil;
import com.vvlive.caibao.smartcity.view.CommonActionBar;
import com.vvlive.caibao.smartcity.view.DateTimePickerDialog;
import com.vvlive.caibao.smartcity.view.DateTimePickerDialog.OnDateTimeSetListener;

import org.kymjs.kjframe.ui.BindView;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/7/3.
 * 出差申请
 */
public class BusinessTripApplyActivity extends BaseActivity {

    @BindView(id = R.id.bta_input)
    EditText ETinput;
    @BindView(id = R.id.bta_start_place)
    EditText ETstartPlace;

    @BindView(id = R.id.bta_set_start_time, click = true)
    RelativeLayout RLsetStartTime;
    @BindView(id = R.id.bta_set_end_time, click = true)
    RelativeLayout RLsetEndTime;

    @BindView(id = R.id.bta_show_start_time)
    TextView TVshowStartTime;
    @BindView(id = R.id.bta_show_end_time)
    TextView TVshowEndTime;

    @BindView(id = R.id.bta_transportation)
    EditText ETtrans;
    @BindView(id = R.id.bta_stay)
    EditText ETstay;

    @BindView(id = R.id.bta_submit, click = true)
    Button BTNsubmit;

    @BindView(id = R.id.bta_about, click = true)
    TextView TVabout;

    private String reason;  // 出差理由
    private String place;   // 出差地点
    private String startTime;  // 出差时间
    private String endTime;   // 返回时间
    private String trans;     // 交通工具
    private String stay;    // 住宿

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_business_trip_apply;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        CommonActionBar actionBar = super.getCommonActionBar();
        actionBar.setMoreVisible(false);
        actionBar.setTitle(R.string.evection);

    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()){
            case R.id.bta_set_start_time:
                // 设置出差时间
                setStartTime();
                break;
            case R.id.bta_set_end_time:
                // 设置出差返回时间
                setEndTime();
                break;
            case R.id.bta_submit:
                // 提交出差申请
                submitApply();
                break;
            case R.id.bta_about:
                // 关于出差的说明
                alert("出差自费。");
                break;
        }
    }

    /**
     * 设置出差时间
     */
    private void setStartTime() {
        DateTimePickerDialog dialog = new DateTimePickerDialog(this, System.currentTimeMillis());
        dialog.setOnDateTimeSetListener(new OnDateTimeSetListener() {
            @Override
            public void OnDateTimeSet(AlertDialog dialog, long date) {
                startTime = TimeUtil.getTargetDateTime(date);
                TVshowStartTime.setText(startTime);
            }
        });
        dialog.show();
    }

    /**
     * 设置出差返回时间
     */
    private void setEndTime() {
        DateTimePickerDialog dialog = new DateTimePickerDialog(this, System.currentTimeMillis());
        dialog.setOnDateTimeSetListener(new OnDateTimeSetListener() {
            @Override
            public void OnDateTimeSet(AlertDialog dialog, long date) {
                endTime = TimeUtil.getTargetDateTime(date);
                TVshowEndTime.setText(endTime);
            }
        });
        dialog.show();
    }

    /**
     * 提交出差申请
     */
    private void submitApply() {
        reason = ETinput.getText().toString();
        place = ETstartPlace.getText().toString();
        trans = ETtrans.getText().toString();
        stay = ETstay.getText().toString();
        String[] arr = {reason, place, startTime, endTime, trans, stay};
        if(canApply(arr)){
            alert(new BusinessTripApply(reason, place, startTime, endTime, trans, stay));
        }else{
            alert("请将出差信息填写完整！");
        }
    }

    private boolean canApply(String...strings) {
        for(String e : strings){
            if(TextUtils.isEmpty(e)){
                return false;
            }
        }
        return true;
    }
}
